package cn.mall.cart.controller;


import cn.mall.cart.config.CartInterceptor;
import cn.mall.cart.service.CartService;
import cn.mall.cart.to.UserInfoTo;
import cn.mall.cart.vo.Cart;
import cn.mall.cart.vo.CartItem;
import cn.mall.common.constant.AuthServerConstant;
import cn.mall.common.constant.MemberResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * 购物车模块
 */
@Controller
@RequestMapping
public class CartController {
    @Autowired
    private CartService cartService;

    //当前用户的购物车信息
    @GetMapping(value = "/currentUserCartItems")
    @ResponseBody
    public List<CartItem> getCurrentCartItems(@RequestParam String cartKey) {
        List<CartItem> cartItemVoList = cartService.getUserCartItems(cartKey);
        for (CartItem item:cartItemVoList){
            Boolean check = item.getCheck();
            if (check == true){
                Long skuId = item.getSkuId();
                cartService.deleteItem(skuId);
            }

        }
        return cartItemVoList;
    }

    /**
     * 用户信息，购物车模块
     *
     * @return
     */
    @GetMapping("/cartList.html")
    public String cartListPage(Model model) throws ExecutionException, InterruptedException {
        Cart cart = cartService.getCart();




        model.addAttribute("cart", cart);
        UserInfoTo userInfoTo = CartInterceptor.toThreadLocal.get();
        System.out.println("controller 拿到的 userInfoTo = " + userInfoTo);
        model.addAttribute("userInfo",userInfoTo);
        return "cartList";
    }

    //添加商品到购物车
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num, RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {
        cartService.addCart(skuId, num);
        redirectAttributes.addAttribute("skuId", skuId);
        return "redirect:/addToCartSuccess.html";
    }

    /**
     * 重定向到添加购物车成功页面，再次刷新不添加，只查询
     *
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccess(@RequestParam("skuId") Long skuId, Model model,HttpSession session) {
        CartItem item = cartService.getCartItem(skuId);
    /*    System.out.println("controller 重定向log " + cartItem.getTotalPrice());
        System.out.println("controller 重定向log " + cartItem.getCount());
        System.out.println("controller 重定向log " + cartItem.getPrice());*/
        if (item.getCount() == null || item.getPrice() == null) {
            System.out.println("没有拿到数据");
        }


        model.addAttribute("item", item);

        return "success";
    }

    /**
     * 选中商品，改变redis数据库信息
     * @param skuId
     * @param check
     * @return
     */
    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,@RequestParam("check") Integer check){
        cartService.checkItem(skuId,check);

        return "redirect:/cartList.html";

    }

    /**
     * 商品数量实时改变 redis数据库
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num){
        cartService.changeItemCount(skuId,num);
        return "redirect:/cartList.html";

    }
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId){
        cartService.deleteItem(skuId);
        return "redirect:/cartList.html";
    }

    /**
     * 为全选设计查询
     * @return
     */
    @GetMapping("/allChecked")
    public String allCheckItem(){
        cartService.allChecked();
        return "redirect:/cartList.html";

    }
}

